<?php

class product{
	
	private $db;
	
	private $id;
	private $name;
	private $url;
	private $carbon;
	private $protein;
	private $fat;
	private $calories;
	
	//array
	private $burns;
	
	
	
	function __construct($name=null,$id=null,$url=null){
		
		try {
				
			$this->db = pg_connect("host='".DB_HOST."' dbname='".DB_NAME."' user='".DB_USER."' password='".DB_PASS."'");
		}catch (Exception $e){
			echo $e->getMessage();	
		}
		//wywolanie odpowiedniej funkcji
		if($name != null && $id == null && $url == null){
			$this->setProductByName($name);
		}
		else if ($id != null && $name == null && $url == null){
			$this->setProductById($id);
		}else if ($url != null && $id == null && $name == null){
			$this->setProductByUrl($url);	
		}else{
			return -1;
		}
		
		$this->burns = $this->getFreeBurnCalories();
			
		
		
		return 0;
	}	
	
	
	function setProductByName($name){
		/*upper stosowany aby wyeleiminowac
		 znaczenie wielkosci liter*/
		try {
		$query = "Select * from products
						where upper(url)  = upper('".$name."');";
		$result = pg_query($this->db,$query);
		$row = pg_fetch_all($result);
		
		}catch (Exception $e){		
			echo $e->getMessage();
		}
				
		$this->id 	= $row[0]['id'];
		$this->name = $row[0]['name_pl'];
		$this->url	= $row[0]['url'];

		$this->calories	= $row[0]['calories'];
		$this->carbon 	= $row[0]['carbon'];
		$this->protein 	= $row[0]['protein'];
		$this->fat 		= $row[0]['fat'];
		//print_r($row);
		return 0;
	}
	
	function setProductById($id){
		
		try {
			$query = "Select * from products
								where id = ".$id."";
		
			$result = pg_query($this->db,$query);
			$row = pg_fetch_all($result);
		}catch (Exception $e){
			echo $e->getMessage();	
		}
		
		$this->id 	= $row[0]['id'];
		$this->name = $row[0]['name_pl'];
		$this->url	= $row[0]['url'];
		
		$this->calories	= $row[0]['calories'];
		$this->carbon 	= $row[0]['carbon'];
		$this->protein 	= $row[0]['protein'];
		$this->fat 		= $row[0]['fat'];
		//print_r($row);
		return 0;
	}
	
	function setProductByUrl($url){

		try {
			$query = "Select * from products
										where url = '".$url."'";
		
			$result = pg_query($this->db,$query);
			$row = pg_fetch_all($result);
		}catch (Exception $e){
			echo $e->getMessage();
		}
		
		$this->id 	= $row[0]['id'];
		$this->name = $row[0]['name_pl'];
		$this->url	= $row[0]['url'];
		
		$this->calories	= $row[0]['calories'];
		$this->carbon 	= $row[0]['carbon'];
		$this->protein 	= $row[0]['protein'];
		$this->fat 		= $row[0]['fat'];
		//print_r($row);
		return 0;
	}
	
	function getFreeBurnCalories(){
		
		try {
		$query = "Select name , kg60 from burn_calories  
					where non_register = 'TRUE'
					order by kg60 desc
		";
		
		$result = pg_query($this->db,$query);
		$rows = pg_fetch_all($result);
		$this->burns = $rows;
		
		}catch (Exception $e){
		echo $e->getMessage();
		}
		
		return $rows;

	}
	
	
	function getId(){
		return $this->id;
	}
	
	function getName(){
		return $this->name;
	}

	function getUrl(){
		return $this->url;
	}
	
	function getCalories(){
		return $this->calories;
	}
	
	function getCarbon(){
		return $this->carbon;
	}
	
	function getProtein(){
		return $this->protein;
	}
	
	function getFat(){
		return $this->fat;
	}
	

}

?>